package zad2;
import java.util.ArrayDeque;
import java.util.Random;

public class Source {
	
	static Random r= new Random(System.nanoTime());

	ArrayDeque<String> values;
	int max;
	
	synchronized public void putNew(String s) {
		while (values.size() >= max)
			try {
				wait();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		synchronized (values) {
			int count = 0;
			if(max - values.size() - 1 <= 0 ){
				count = 1;
			}
			else{
				count =1 + r.nextInt((max - values.size() - 1));
			}
			for (int i = 0; i < count; i++) {
				values.push(s + i);
			}
			System.out.println("produkowanie: " + count + "");
			
			System.out.println("++++++++++++++++++++++++ w kolejce : " + values.size());
		}
		notifyAll();

	}

	synchronized public String get() {
		
		
		while (values.size() == 0) {
			try {
				wait();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}			
		}
		String val;
		synchronized (values) {
			
			String out = "";
			
			int count =0;
			if(values.size()<= 0 ){
				count = 1;
			}
			else{
				count = r.nextInt(values.size() - 1);
			}
			
			for (int i = 0; i < count; i++) {
				out += values.pollFirst();
			}
			System.out.println("konsumowanie: " + count + "");

			System.out.println("------------------------ w kolejce : " + values.size());
			
			val = out;
		}
		notifyAll();
		return val;
	}

	public Source(int maxValues) {
		values = new ArrayDeque<String>();
		max = maxValues;
	}
	
}
